package com.veo.controller;

import com.github.pagehelper.PageInfo;
import com.veo.domain.Product;
import com.veo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/productController")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/findAllProduct")
    public ModelAndView findAllProduct(@RequestParam(value = "page",defaultValue = "1",required = false)Integer page,@RequestParam(value = "size",defaultValue = "5",required = false)Integer size,@RequestParam(value = "searcherName",defaultValue = "",required = false)String searcherName) throws UnsupportedEncodingException {
        ModelAndView mv=new ModelAndView();
        //判断是乱码 (GBK包含全部中文字符；UTF-8则包含全世界所有国家需要用到的字符。)
        if (!(Charset.forName("GBK").newEncoder().canEncode(searcherName))) {
            //转码UTF8
            searcherName = new String(searcherName.getBytes("ISO-8859-1"), "utf-8");
        }
        List<Product> products = productService.findAllProduct(page,size,searcherName);
        //保存分页信息
        PageInfo productList=new PageInfo(products);
        mv.addObject("productList",productList);
        mv.addObject("searcherName",searcherName);
        mv.setViewName("product-list");
        return mv;
    }


    @RequestMapping("/findProductById")
    public ModelAndView findProductById(String id){
        ModelAndView mv=new ModelAndView();
        Product product = productService.findProductById(id);
        mv.addObject("updateProduct",product);
        mv.setViewName("product-update");
        return mv;
    }

    @RequestMapping("/updateProduct")
    public String updateProduct(Product product){
        ModelAndView mv=new ModelAndView();
        int count = productService.updateProduct(product);
        if (count>0){
            return "redirect:findAllProduct";
        }
        else {
            return "error";
        }
    }

    @RequestMapping("/deleteProductById")
    public String deleteProductById(String id){
        int count = productService.deleteProductById(id);
        if (count>0){
            return "redirect:findAllProduct";
        }
        return "error";
    }

    @RequestMapping("/deleteProductByIds")
    public String deleteProductByIds(String idStr){
        if(idStr!=null && idStr.length()>0 && idStr!=""){
            String[] ids=idStr.split(",");
            for (String id : ids) {
                productService.deleteProductById(id);
            }
        }
        return "redirect:findAllProduct";
    }


    @RequestMapping("/addProduct")
    public String addProduct(Product product){
        String id = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        product.setId(id);
        int count = productService.addProduct(product);
        if (count>0){
            return "redirect:findAllProduct";
        }
        return "error";
    }

    @RequestMapping("/updateProductStatus")
    public String updateProductStatus(@RequestParam(value = "status",required = true) String status,@RequestParam(value = "idStr",required = true) String idStr){
        if (Integer.parseInt(status)==1){
            if (idStr!=null && idStr.length()>0&&idStr!=""){
                String[] ids = idStr.split(",");
                System.out.println(ids.length);
                for (String id : ids) {
                    productService.updateProductStatus(id,Integer.parseInt(status));
                }
            }
        }else if (Integer.parseInt(status)==0){
            if (idStr!=null && idStr.length()>0&&idStr!=""){
                String[] ids = idStr.split(",");
                for (String id : ids) {
                    productService.updateProductStatus(id,Integer.parseInt(status));
                }
            }
        }
        return "redirect:findAllProduct";
    }
}
